package com.yenn.barcode.zxingdemo;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * #### 6-3 使用zxing进行二维码解析 (05:20)
 */
@Slf4j
public class ReadQRCode {
    public static void main(String[] args) {


        try {
            File file = new File("D:"+File.separator+"qr.png");
            BufferedImage image = ImageIO.read(file);
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));

            // 定义二维码的参数
            HashMap<DecodeHintType,Object> hints = new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET,"utf-8");

            MultiFormatReader formatReader = new MultiFormatReader();
            Result result = formatReader.decode(binaryBitmap, hints);
            log.info("解析结果：{}",result);

            log.info("二维码格式类型: {}",result.getBarcodeFormat()) ;
            log.info("二维码文本内容: {}",result.getText());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }


    }
}
